package xiaodou.com.testinterface.net;
/*
 *  项目名：TestInterface
 *  包名：xiaodou.com.testinterface.net
 *  创建者：SKFsky
 *  创建时间：2017/11/16 13:48
 *  描述:   封装OkHttp进行网络请求
 */

import android.os.Handler;
import android.util.Log;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpManager {
    private static OkHttpManager instance;
    private OkHttpClient mOkHttpClient;
    private static Handler okHandler;

    public OkHttpManager(){
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(15, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS);
        mOkHttpClient = builder.build();
    }

    /**
     * 实例化OkHttpManager这个类
     * @return
     */
    public static  OkHttpManager getInstance(){
        if (instance==null){
            synchronized (OkHttpManager.class){
                if (instance==null){
                    instance = new OkHttpManager();
                    okHandler = new Handler();
                }
            }
        }
        return instance;
    }

    /**
     * GET请求
     */
    public void getNet(String url,ResultCallback resultCallBack){
        Log.i("okhttp","url--"+url);
        Request request = new Request.Builder()
                .url(url)
                .method("GET",null)
                .build();
        dealNet(request,resultCallBack);
    }

    /**
     * POST请求
     * @param url
     * @param resultCallback
     * @param param
     */
    public void postNet(String url, ResultCallback resultCallback, Param... param){
        if (param == null){
            param = new Param[0];
        }
        FormBody.Builder formBody = new FormBody.Builder();
        for (Param p : param){
            formBody.add(p.key,p.value);
        }
        RequestBody requestBody = formBody.build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        dealNet(request,resultCallback);
    }

    private void dealNet(final Request request, final ResultCallback resultCallBack) {
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                okHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        resultCallBack.onFailed(request,e);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = "";
                str = response.body().string();
                final String finalStr = str;
                okHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        resultCallBack.onSuccess(finalStr);
                    }
                });
            }
        });

    }

    /**
     * 回调处理请求结果
     */
    public static abstract class ResultCallback{
        public abstract void onFailed(Request request, IOException e);

        public abstract void onSuccess(String response);
    }

    /**
     * 设置POST的请求参数
     */
    public static class Param{
        String key;
        String value;

        public Param(){

        }

        public Param(String key,String value){
            this.key = key;
            this.value = value;
        }
    }
}
